package com.yoooya.base.utils;

import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 * 
 * @author Sun Yijing<br />
 *         Copyright © Xiamen Yoooya NetWorks Co.,Ltd.
 * @since yoooya software platform v1.0.0
 * @version 1.0.0 <br />
 *          <table border="1" cellpadding="3" cellspacing="0">
 *          <tr>
 *          <th align="left" colspan="3"><b>修改历史</b></th>
 *          </tr>
 *          <tr>
 *          <td>修改日期</td>
 *          <td>修改人</td>
 *          <td>修改描述</td>
 *          </tr>
 *          <tr>
 *          <td>2018-6-20</td>
 *          <td>Sun Yijing</td>
 *          <td>新建</td>
 *          </tr>
 *          </table>
 */
public class ListPageUtil<T> {

	/**
	 * 每页显示条数
	 */
	private int pageSize;

	/**
	 * 总页数
	 */
	private int pageCount;

	/**
	 * 原集合
	 */
	private List<T> data;

	public ListPageUtil(List<T> data, int pageSize) {
		if (data == null || data.isEmpty()) {
			this.data = Collections.emptyList();
			this.pageSize = pageSize;
			this.pageCount = 0;
		}
		this.data = data;
		this.pageSize = pageSize;
		this.pageCount = data.size() / pageSize;
		if (data.size() % pageSize != 0) {
			this.pageCount++;
		}
	}

	/**
	 * 得到分页后的数据
	 *
	 * @param pageNum
	 *            页码
	 * @return 分页后结果
	 */
	public List<T> getPagedList(int pageNum) {
		int fromIndex = (pageNum - 1) * pageSize;
		if (fromIndex >= data.size()) {
			return Collections.emptyList();
		}

		int toIndex = pageNum * pageSize;
		if (toIndex >= data.size()) {
			toIndex = data.size();
		}
		return data.subList(fromIndex, toIndex);
	}

	public int getPageSize() {
		return pageSize;
	}

	public List<T> getData() {
		return data;
	}

	public int getPageCount() {
		return pageCount;
	}

}